package com.trailwalker.inputmethod.dialog;

import com.trailwalker.inputmethod.service.data.InputMethodDataService;
import com.trailwalker.inputmethod.service.entity.InputMethodKeys;
import com.trailwalker.javafx.utils.image.ImageUtils;
import com.trailwalker.javafx.utils.style.AdaptUtils;
import com.trailwalker.javafx.utils.style.StyleUtils;
import javafx.event.EventHandler;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.Cursor;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.TextField;
import javafx.scene.image.ImageView;
import javafx.scene.input.MouseEvent;
import javafx.scene.layout.*;
import javafx.scene.paint.Color;
import javafx.scene.paint.Paint;
import javafx.scene.text.Font;
import javafx.stage.Modality;
import javafx.stage.Stage;
import javafx.stage.StageStyle;

public class EditDialog extends BaseDialog {

    Stage primaryStage = null;
    private double dialogStageHeight;
    private double dialogStageWidth;
    private Stage dialogStage;
    private BorderPane bpParent;
    private ImageView ivClose;
    private Button btnConfirm;
    private TextField tfNickName;
    private TextField tfDesc;
    private TextField tfBatchGroup;
    private Label lblNickName;
    private Label lblDesc;
    private Label lblBatchGroup;
    private AnchorPane apParentTop;
    private InputMethodDataService inputMethodDataService;
    private InputMethodKeys mInputMethodKeys;
    private Label lblTitle;


    public EditDialog(Stage primaryStage) {
        this.primaryStage = primaryStage;
        double primaryStageHeight = primaryStage.getHeight();
        double primaryStageWidth = primaryStage.getWidth();
        dialogStageHeight = primaryStageHeight / 4;
        dialogStageWidth = primaryStageWidth / 4;

        dialogStage = new Stage();
        dialogStage.setHeight(dialogStageHeight);
        dialogStage.setWidth(dialogStageWidth);
        dialogStage.setX(primaryStage.getX() + primaryStageWidth / 2 - dialogStageWidth / 2);
        dialogStage.setY(primaryStage.getY() + primaryStageHeight / 2 - dialogStageHeight / 2);
        //窗口设置
        dialogStage.initOwner(primaryStage);
        dialogStage.initStyle(StageStyle.TRANSPARENT);
        dialogStage.initModality(Modality.APPLICATION_MODAL);

        bpParent = new BorderPane();
        initScene();
    }

    public void showDialog() {

        Scene scene = new Scene(bpParent);
        scene.setFill(null);
        dialogStage.setScene(scene);
        dialogStage.showAndWait();
    }

    private void initScene() {
        bpParent.setBackground(StyleUtils.getBackGround("#dddddd"));
        initLoad();
        initView();
        initData();
        initListener();
        onReady();
    }

    private void initLoad() {
        inputMethodDataService = new InputMethodDataService();
    }

    private void onReady() {
        setMoveAble(dialogStage, apParentTop);
    }

    private void initListener() {
        if (ivClose != null) {
            ivClose.setOnMouseClicked(new EventHandler<MouseEvent>() {
                @Override
                public void handle(MouseEvent event) {
                    inputMethodDataService.release();
                    dialogStage.close();
                }
            });
        }

        if (btnConfirm != null) {
            btnConfirm.setOnMouseClicked(new EventHandler<MouseEvent>() {
                @Override
                public void handle(MouseEvent event) {
                    mInputMethodKeys.setNiceName(tfNickName.getText());
                    mInputMethodKeys.setDesc(tfDesc.getText());
                    mInputMethodKeys.setBatchGroup(tfBatchGroup.getText());
                    inputMethodDataService.updateInputMethodKeys(mInputMethodKeys);
                    dialogStage.close();
                }
            });
        }

    }

    private void initData() {

    }

    private void initView() {
        //初始化顶部控件
        apParentTop = new AnchorPane();
        apParentTop.setBackground(StyleUtils.getBackGround("#026CBA"));
        apParentTop.setPrefHeight(dialogStageHeight / 5);
        lblTitle = new Label("编辑");
        lblTitle.setFont(Font.font(dialogStageHeight / 15));
        lblTitle.setTextFill(Color.WHITE);
        apParentTop.getChildren().add(lblTitle);
        AnchorPane.setTopAnchor(lblTitle, dialogStageHeight / 20);
        AnchorPane.setBottomAnchor(lblTitle, dialogStageHeight / 20);
        AnchorPane.setLeftAnchor(lblTitle, dialogStageHeight / 20);
        ivClose = ImageUtils.getImageView(this,20, "close_normal.png", "close_press.png");
        ivClose.setFitHeight(dialogStageHeight / 10);
        ivClose.setFitWidth(dialogStageHeight / 10);
        ivClose.setCursor(Cursor.HAND);
        apParentTop.getChildren().add(ivClose);
        AnchorPane.setTopAnchor(ivClose, dialogStageHeight / 20);
        AnchorPane.setBottomAnchor(ivClose, dialogStageHeight / 20);
        AnchorPane.setRightAnchor(ivClose, dialogStageHeight / 20);
        bpParent.setTop(apParentTop);

        //初始化中间控件
        VBox vbParentCenter = new VBox();
        vbParentCenter.setAlignment(Pos.CENTER);
        vbParentCenter.setSpacing(AdaptUtils.getInstance().getScale(10));
        //hbParentCenter.setBackground(StyleUtils.getBackGround("#ff0000"));
        HBox hbNickNameContainer = new HBox();
        //vbParentCenter.setBackground(StyleUtils.getBackGround("#ff0000"));
        hbNickNameContainer.setPrefWidth(dialogStageWidth / 2);
        lblNickName = new Label("nickName:");
        lblNickName.setFont(Font.font(dialogStageHeight / 15));
        lblNickName.setTextFill(Color.BLACK);
        lblNickName.setAlignment(Pos.CENTER_LEFT);
        AdaptUtils.getInstance().setScaleWidth(lblNickName, 100);
        hbNickNameContainer.getChildren().add(lblNickName);
        HBox hbTfNickNameContainer = new HBox();
        hbTfNickNameContainer.setPrefHeight(dialogStageHeight / 8);
        hbTfNickNameContainer.setBorder(new Border(new BorderStroke(Paint.valueOf("#026CBA"), BorderStrokeStyle.SOLID, CornerRadii.EMPTY, new BorderWidths(2))));
        tfNickName = new TextField();
        tfNickName.setPrefHeight(dialogStageHeight / 8);
        tfNickName.setFont(Font.font(dialogStageHeight / 15));
        tfNickName.setPadding(new Insets(0));
        tfNickName.setStyle("-fx-text-inner-color: black;");
        tfNickName.setBackground(new Background(new BackgroundFill(Paint.valueOf("#ffffff00"), CornerRadii.EMPTY, new Insets(0))));
        hbTfNickNameContainer.getChildren().add(tfNickName);
        hbTfNickNameContainer.setAlignment(Pos.CENTER_LEFT);
        HBox.setHgrow(tfNickName, Priority.ALWAYS);
        HBox.setMargin(tfNickName, new Insets(0, dialogStageHeight / 20, 0, dialogStageHeight / 20));
        hbNickNameContainer.getChildren().add(hbTfNickNameContainer);
        hbNickNameContainer.setAlignment(Pos.CENTER_LEFT);
        hbNickNameContainer.setSpacing(dialogStageHeight / 15);
        vbParentCenter.getChildren().add(hbNickNameContainer);

        HBox hbDescContainer = new HBox();
        //vbParentCenter.setBackground(StyleUtils.getBackGround("#ff0000"));
        hbDescContainer.setPrefWidth(dialogStageWidth / 2);
        lblDesc = new Label("desc:");
        lblDesc.setFont(Font.font(dialogStageHeight / 15));
        lblDesc.setTextFill(Color.BLACK);
        lblDesc.setAlignment(Pos.CENTER_LEFT);
        AdaptUtils.getInstance().setScaleWidth(lblDesc, 100);
        hbDescContainer.getChildren().add(lblDesc);
        HBox hbTfDescContainer = new HBox();
        hbTfDescContainer.setPrefHeight(dialogStageHeight / 8);
        hbTfDescContainer.setBorder(new Border(new BorderStroke(Paint.valueOf("#026CBA"), BorderStrokeStyle.SOLID, CornerRadii.EMPTY, new BorderWidths(2))));
        tfDesc = new TextField();
        tfDesc.setPrefHeight(dialogStageHeight / 8);
        tfDesc.setFont(Font.font(dialogStageHeight / 15));
        tfDesc.setPadding(new Insets(0));
        tfDesc.setStyle("-fx-text-inner-color: black;");
        tfDesc.setBackground(new Background(new BackgroundFill(Paint.valueOf("#ffffff00"), CornerRadii.EMPTY, new Insets(0))));
        hbTfDescContainer.getChildren().add(tfDesc);
        hbTfDescContainer.setAlignment(Pos.CENTER_LEFT);
        HBox.setHgrow(tfDesc, Priority.ALWAYS);
        HBox.setMargin(tfDesc, new Insets(0, dialogStageHeight / 20, 0, dialogStageHeight / 20));
        hbDescContainer.getChildren().add(hbTfDescContainer);
        hbDescContainer.setAlignment(Pos.CENTER_LEFT);
        hbDescContainer.setSpacing(dialogStageHeight / 15);
        vbParentCenter.getChildren().add(hbDescContainer);

        HBox hbBatchGroupContainer = new HBox();
        //vbParentCenter.setBackground(StyleUtils.getBackGround("#ff0000"));
        hbBatchGroupContainer.setPrefWidth(dialogStageWidth / 2);
        lblBatchGroup = new Label("batchGroup:");
        lblBatchGroup.setFont(Font.font(dialogStageHeight / 15));
        lblBatchGroup.setTextFill(Color.BLACK);
        lblBatchGroup.setAlignment(Pos.CENTER_LEFT);
        AdaptUtils.getInstance().setScaleWidth(lblBatchGroup, 100);
        hbBatchGroupContainer.getChildren().add(lblBatchGroup);
        HBox hbTfBatchGroupContainer = new HBox();
        hbTfBatchGroupContainer.setPrefHeight(dialogStageHeight / 8);
        hbTfBatchGroupContainer.setBorder(new Border(new BorderStroke(Paint.valueOf("#026CBA"), BorderStrokeStyle.SOLID, CornerRadii.EMPTY, new BorderWidths(2))));
        tfBatchGroup = new TextField();
        tfBatchGroup.setPrefHeight(dialogStageHeight / 8);
        tfBatchGroup.setFont(Font.font(dialogStageHeight / 15));
        tfBatchGroup.setPadding(new Insets(0));
        tfBatchGroup.setStyle("-fx-text-inner-color: black;");
        tfBatchGroup.setBackground(new Background(new BackgroundFill(Paint.valueOf("#ffffff00"), CornerRadii.EMPTY, new Insets(0))));
        hbTfBatchGroupContainer.getChildren().add(tfBatchGroup);
        hbTfBatchGroupContainer.setAlignment(Pos.CENTER_LEFT);
        HBox.setHgrow(tfBatchGroup, Priority.ALWAYS);
        HBox.setMargin(tfBatchGroup, new Insets(0, dialogStageHeight / 20, 0, dialogStageHeight / 20));
        hbBatchGroupContainer.getChildren().add(hbTfBatchGroupContainer);
        hbBatchGroupContainer.setAlignment(Pos.CENTER_LEFT);
        hbBatchGroupContainer.setSpacing(dialogStageHeight / 15);
        vbParentCenter.getChildren().add(hbBatchGroupContainer);

        bpParent.setCenter(vbParentCenter);

        //初始化底部控件
        HBox hbParentBottom = new HBox();
        hbParentBottom.setPrefHeight(dialogStageHeight / 3);
        //hbParentBottom.setBackground(StyleUtils.getBackGround("#00ff00"));
        btnConfirm = new Button("确定");
        btnConfirm.setCursor(Cursor.HAND);
        btnConfirm.setPrefWidth(dialogStageHeight / 3);
        btnConfirm.setPrefHeight(dialogStageHeight / 7);
        btnConfirm.setBackground(new Background(new BackgroundFill(Paint.valueOf("#026CBA"), new CornerRadii(dialogStageHeight / 14), new Insets(0))));
        btnConfirm.setFont(Font.font(dialogStageHeight / 15));
        btnConfirm.setTextFill(Paint.valueOf("#ffffff"));
        hbParentBottom.getChildren().add(btnConfirm);
        hbParentBottom.setAlignment(Pos.CENTER);
        bpParent.setBottom(hbParentBottom);
    }

    public void setInputMethodKeys(InputMethodKeys inputMethodKeys) {
        mInputMethodKeys = inputMethodKeys;
        tfNickName.setText(mInputMethodKeys.getNiceName());
        tfDesc.setText(mInputMethodKeys.getDesc());
        tfBatchGroup.setText(mInputMethodKeys.getBatchGroup());
        lblTitle.setText("编辑 " + mInputMethodKeys.getName());
    }
}
